package com.agile.leetcode.node.slowfastpointer;

/**
 * k快慢指针的demo
 *
 * @Author ChenZhangKun
 * @Date 2020/12/23 10:20
 */
public class SlowFastPointer {
    public static void main(String[] args) {
        Node node=new Node(-1);
        for (int i = 0; i < 100; i++) {
            node.add(new Node(i));
        }
        System.out.println(node.next.slowAndFastTraverse());
    }
}
class Node{
    // 值
    int value;
    // 下一个
    Node next;
    public void add(Node node){
        Node currentNode=this;
        while (currentNode.next!=null){
            currentNode=currentNode.next;
        }
        currentNode.next=node;
    }

    public Node(int value) {
        this.value = value;
    }

    @Override
    public String toString() {
        return "Node{" +
                "value=" + value +
                ", next=" + next +
                '}';
    }
    public int slowAndFastTraverse(){
        Node slow=this;
        Node fast=this;
        while (slow!=null&&fast!=null){
            slow=slow.next;
            fast=fast.next.next;
        }
        return slow.value;
    }
}
